# coding=utf-8
"""
Created on 2018年9月12日
"""
from common_base.constants import TMsgType
from common_base.constants import Platform
import json
from service_base.aws_sns import AwsSNSService


# MessageTypeFollow = 1                               # 关注
# MessageTypeOthersComment = 2                        # 他人评论
# MessageTypeSelfComment = 3                          # 自己评论
# MessageTypeLike = 4                                 # 点赞
# MessageTypeArticleDeleted = 5                       # 删除帖子
# MessageTypeArticleAvailable = 6                     # rendering succeeded
# MessageTypeArticleFailed = 7                        # rendering failed
# MessageTypeFacebookFriendFollow = 10                # facebook friend follow

class PushService(object):
    """
    """
    def __init__(self, processor=None):
        """
        """
        self._processor = processor
        self.sns_service = AwsSNSService()

    def notify(self, push_data):
        """
        发送
        """
        message_builder_service = MessageBuilder()
        for each_push_data in push_data:
            # 组装push的基本数据
            endpoint_arn = each_push_data['endpointArn']
            payload_json = message_builder_service.get_publish_param(each_push_data)
            # 发送push
            self.sns_service.sns_publish(endpoint_arn, payload_json)

    def get_push_info_with_userids(self):
        """
        获取push的相关信息
        """

    # def get_setting_pos(self, msg_type):
    #     """
    #     获取setting pos
    #     为兼容老接口设计
    #     """
    #     setting_pos = 0
    #     if msg_type == (TMsgType.Follow, TMsgType.FacebookFriendFollow):
    #         setting_pos = 1
    #     elif msg_type in (TMsgType.OthersComment, TMsgType.SelfComment):
    #         setting_pos = 2
    #     elif msg_type == TMsgType.Like:
    #         setting_pos = 3
    #     elif msg_type == TMsgType.ArticleDeleted:
    #         setting_pos = 4
    #     elif msg_type == (TMsgType.ArticleAvailable, TMsgType.ArticleFailed):
    #         setting_pos = 5
    #
    #     return setting_pos


class MessageBuilder(object):
    """
    """

    def __init__(self):
        """
        """

    def get_publish_param(self, push_info):
        """
        发送push的基本参数
        """
        payload = dict(
            default='Alive'
        )
        if self._get_platform(push_info['appId']) == Platform.Ios:
            APNS = dict(
                articleId=push_info['articleId'],
                userKey=push_info['userKey'],
                msgId=push_info['msgId'],
                msgType=push_info['msgType']
            )
            APNS['aps'] = dict(
                alert=push_info['message'],
                badge=push_info['badge'],
            )
            if push_info.get('sound'):
                APNS['aps']['sound'] = 'default'

            payload['APNS'] = json.dumps(APNS)

        else:
            gcm = dict(
                message=push_info['message'],
                badge=push_info['badge'],
                articleId=push_info['articleId'],
                userKey=push_info['userKey'],
                msgId=push_info['msgId'],
                msgType=push_info['msgType']
            )
            GCM = dict(
                data=gcm
            )

            payload['GCM'] = json.dumps(GCM)

        payload_json = json.dumps(payload)

        return payload_json

    def _get_platform(self, appid):
        """
        获取平台
        """
        platform = Platform.Ios if 'ios' in appid.lower() else Platform.Android
        return platform


if __name__ == '__main__':
    push_info = dict(
        message='小岳岳给你点赞了',
        badge=12,
        articleId=6644565,
        # userKey='9c6f1f688f0711e88f97068721ef9ff4',
        userKey='5f72db50baf011e89e4d068721ef9ff4',
        msgId=12,
        msgType=4,
        endpointArn='arn:aws:sns:us-west-2:085307523710:endpoint/APNS/AliveService-iOS/086df865-f89f-3342-a069-08b1ab84e870',
        appId='ios.alivemovie.com'
    )
    PushService().notify([push_info])